

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import '../main.dart';

class FreshState extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _FreshState();

}
class _FreshState extends State<FreshState> {
  late EasyRefreshController _controller;
  late ScrollController _scrollController;

  // 条目总数
  int _count = 20;

  // 反向
  bool _reverse = false;

  // 方向
  Axis _direction = Axis.vertical;

  // Header浮动
  bool _headerFloat = false;

  // 无限加载
  bool _enableInfiniteLoad = true;

  // 控制结束
  bool _enableControlFinish = false;

  // 任务独立
  bool _taskIndependence = false;

  // 震动
  bool _vibration = true;

  // 是否开启刷新
  bool _enableRefresh = true;

  // 是否开启加载
  bool _enableLoad = true;

  // 顶部回弹
  bool _topBouncing = true;

  // 底部回弹
  bool _bottomBouncing = true;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
    _scrollController = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('刷新框架'),
      ),
      body: Center(
        child: Container(
          height: _direction == Axis.vertical ? double.infinity : 210.0,
          child: EasyRefresh.custom(
            enableControlFinishRefresh: true,
            enableControlFinishLoad: true,
            // taskIndependence: _taskIndependence,
            controller: _controller,
            // scrollController: _scrollController,
            // reverse: _reverse,
            // scrollDirection: _direction,
            // topBouncing: _topBouncing,
            // bottomBouncing: _bottomBouncing,
            header: _enableRefresh
                ? MaterialHeader()
                : null,
            footer: _enableLoad
                ? ClassicalFooter(
              enableInfiniteLoad: _enableInfiniteLoad,
              enableHapticFeedback: _vibration,
              loadText: '拉动加载',
              loadReadyText: '释放加载',
              loadingText: '正在加载...',
              loadedText: '加载完成',
              loadFailedText: '加载失败',
              noMoreText: '没有更多数据',
              infoText: '更新时间 %T',
            )
                : null,
            onRefresh: _enableRefresh
                ? () async {
              await Future.delayed(Duration(seconds: 2), () {
                if (mounted) {
                  setState(() {
                    _count = 20;
                  });
                  if (!_enableControlFinish) {
                    _controller.resetLoadState();
                    _controller.finishRefresh();
                  }
                }
              });
            }
                : null,
            onLoad: _enableLoad
                ? () async {
              await Future.delayed(Duration(seconds: 2), () {
                if (mounted) {
                  setState(() {
                    _count += 20;
                  });
                  if (!_enableControlFinish) {
                    _controller.finishLoad(noMore: _count >= 80);
                  }
                }
              });
            }
                : null,
            slivers: <Widget>[
              SliverList(
                delegate: SliverChildBuilderDelegate(
                      (context, index) {
                    return ListTile(
                      title: Text('index '+index.toString()),
                      onTap: (){
                        print('click '+index.toString());
                      },
                    );
                  },
                  childCount: _count,
                ),
              ),
            ],
          ),
        ),
      ),

    );
  }

}